import * as api from "@/api/permission";
//引入需要校验的路由
import allRoutes from "@/router/generateRouter";
//把用递归创建用户路由的方法引入
import recusionRoutes from "@/router/recusionRoutes";
// //引入路由
import router from "@/utils/routerGuard";
// //引入动态路由
import dynamicRoutes from "@/router/dynamicRoutes";
let routes = allRoutes.find((route) => route.name === "home");
export default {
  namespaced: true,
  state: {
    userMenu: [],
  },
  getters: {},
  mutations: {
    //设置用户菜单
    SET_USER_MENU(state, payload) {
      state.userMenu = [...payload];
      //动态添加路由
      dynamicRoutes.children = [...payload];
      dynamicRoutes.children.push({
        path: "/preorder",
        component: () => import("@/pages/home/PreOrder"),
      });
      dynamicRoutes.children.push({
        path: "/payment/pay",
        name: "home-payment-pay",
        component: () => import("@/pages/home/PayMent/Pay"),
      });
      dynamicRoutes.children.push({
        path: "/payment/paysuccess",
        component: () => import("@/pages/home/PayMent/PaySuccess"),
      });

      router.addRoute(dynamicRoutes);
    },
  },
  actions: {
    //获取远程菜单
    FETCH_MENU_LIST(context) {
      return api.menuList().then((res) => {
        // console.log(res.data.menuList, "123");
        let userMenu = recusionRoutes(res.data.menuList, routes.children);
        // console.log(userMenu);
        context.commit("SET_USER_MENU", userMenu);
      });
    },
  },
  modules: {},
};
